package uestc.lj.advanced.slidingwindow;

/**
 * @Author:Crazlee
 * @Date:2021/12/9
 */
public class Code01_LongestSumSubArrayLengthInPositiveArray {
	public static int getMaxLength(int[] arr, int k) {
		if (arr == null || arr.length == 0 || k <= 0) {
			return 0;
		}
		int left = 0;
		int right = 0;
		int sum = arr[0];
		int ans = 0;
		while (right < arr.length) {
			if (sum == k) {
				ans = Math.max(ans, right - left + 1);
				sum -= arr[left++];
			} else if (sum < k) {
				right++;
				if (right == arr.length) {
					break;
				}
				sum += arr[right];
			} else {
				sum -= arr[left++];
			}
		}
		return ans;
	}
}
